import { Engine } from "../engine/engine";
import { FundAccount } from '../engine/funds/fundAccount.js';
import { Store } from '../engine/store/store.js';
import { DocType, CommonOrderStatus, OrderInventoryHandleMethod, OrderSortType } from '../engine/common/docDefinition.js';
import { StoreRetailSettings } from '../engine/retail/storeRetailSettings.js';
import { TargetObject } from '../engine/targetOrg/targetObject.js';
//import { LogUtil } from './log_util.js';
import { WareHouse } from '../engine/inventory/warehouse.js';
import {InventoryInOutType} from '../engine/inventory/inventory.js';
import {InventoryType} from '../engine/inventory/inventoryType.js';

export class OrderUtil {

    /// json 转换为 ThirdpartyCode
    static jsonToDocType(jsonValue,
         defaultValue = DocType.Unknown ) {
        if (jsonValue == null) {
            return defaultValue;
        } else {
            let retValue = defaultValue;
            try {
                retValue = DocType.fromCode(jsonValue);
            } catch (err) {
                console.log("Convert jsonToDocType string failed:$err");
            }
            return retValue;
        }
    }

    // 获取销售单据快捷操作配置
    static getSalesOrderInentoryHandleMethod() {
        let _quickInventoryMode;
        switch (Engine.getOrgSettings().salesOrderInventoryHandleMethod) {
            case OrderInventoryHandleMethod.QuickInventory:
                _quickInventoryMode = true;
                break;
            case OrderInventoryHandleMethod.WaitInventory:
                _quickInventoryMode = false;
                break;
            case OrderInventoryHandleMethod.InventoryBySetting:
                _quickInventoryMode = false;
                break;
            default:
                _quickInventoryMode = true;
                break;
        }
        return _quickInventoryMode;
    }

    // 获取采购单据快捷操作配置
    static getPurchaseOrderInentoryHandleMethod() {
        let _quickInventoryMode;
        switch (Engine.getOrgSettings().purchaseOrderInventoryHandleMethod) {
            case OrderInventoryHandleMethod.QuickInventory:
                _quickInventoryMode = true;
                break;
            case OrderInventoryHandleMethod.WaitInventory:
                _quickInventoryMode = false;
                break;
            case OrderInventoryHandleMethod.InventoryBySetting:
                _quickInventoryMode = false;
                break;
            default:
                _quickInventoryMode = true;
                break;
        }
        return _quickInventoryMode;
    }

    // 获取单据初始化仓库
    // 如果存在零售默认仓库，使用默认仓库，没有则返回空仓库
    static getOrderDefaultWarehouse({ storeId, curWarehouse }) {
        if (storeId && Engine.getOrgSettings().autoFillOrderWithDefaultWarehouse) {
            if (curWarehouse.id) {
                // 已有仓库不再重复指定
                return curWarehouse;
            } else {
                const warehouse = OrderUtil.getStoreDefaultWarehouse(storeId);

                if (warehouse && warehouse.id) {
                    return warehouse;
                } else {
                    return curWarehouse;
                }
            }
        } else {
            // 未开启指定默认仓库功能，不切换仓库
            return curWarehouse;
        }
    }

    // 获取默认出入库类型
    // inoutType 类型 InventoryInOutType
    static getDefaultInventoryType(inoutType) { 
        if(inoutType === InventoryInOutType.In){
            // todo 注意 这个XSRK 拼写有错误，如果后台调整，这里也要调整
            return new InventoryType({name:"采购入库",code:"XSRK",inOutType:inoutType})
        }
        else if(inoutType === InventoryInOutType.Out){
            return new InventoryType({name:"销售出库",code:"XSCK",inOutType:inoutType})
        }
        else {
            // 抛出异常
            throw new Error("无效的入出库类型");   
        }
    }

    // 获取门店默认仓库
    static getStoreDefaultWarehouse(storeId) {
        const store = new Store({ id: storeId });
        const storeRetailSettings = Engine.getRetailSettings().getStoreRetailInventorySettings(store);

        const warehouse = storeRetailSettings.wareHouse;
        if (warehouse && warehouse.id) {
            return warehouse;
        }
        return null;
    }

    // 获取单据初始化账户
    // 如果存在零售默认账户，使用默认账户，没有则返回空账户
    static getOrderDefaultFundAccount({storeId, curFundAccount}) {
        if (storeId && Engine.getOrgSettings().autoFillOrderWithDefaultAccount) {
            if (curFundAccount?.id) {
                // 已有账户不再重复指定
                return curFundAccount;
            } else {
                const fundAccount = OrderUtil.getStoreDefaultFundAccount(storeId);

                if (fundAccount && fundAccount.id) {
                    return fundAccount;
                } else {
                    return curFundAccount;
                }
            }
        } else {
            // 未开启指定默认账户功能，不切换账户
            return curFundAccount;
        }
    }

    // 获取门店资金账户
    static getStoreDefaultFundAccount(storeId) {
        const store = new Store({ id: storeId });
        const storeRetailSettings = Engine.getRetailSettings().getStoreRetailInventorySettings(store);

        const fundAccount = storeRetailSettings.cashAccount.fundAccount;
        if (fundAccount && fundAccount.id) {
            return fundAccount;
        }
        return null;
    }

    // 获取默认客户
    static getOrderDefaultCustomer(curCustomer) {
        if (Engine.getOrgSettings().autoFillOrderWithDefaultCustomer && Engine.profile.org.defaultCustomer) {
            return Engine.profile.org.defaultCustomer;
        } else {
            return curCustomer;
        }
    }

    // 获取默认供应商
    static getOrderDefaultSupplier(curSupplier) {
        if (Engine.getOrgSettings().autoFillOrderWithDefaultSupplier && Engine.profile.org.defaultSupplier) {
            return Engine.profile.org.defaultSupplier;
        } else {
            return curSupplier;
        }
    }

    // 销售单相关权限
    static hasSalesRelatedOrderReadPermission() {
        return Engine.profile.permissionMgr.hasPermission([
            FunctionPermissionDefinition.P_SalesContract_Read,
            FunctionPermissionDefinition.P_SalesReturn_Read,
        ]);
    }

    // 销售单相关权限
    static hasSalesRelatedOrderEditPermission() {
        if (Engine.getOrgSettings().enableSalesRelatedOrdersQuickMode) {
            // 快捷模式下，编辑和审核同等权利
            return Engine.profile.permissionMgr.hasPermission([
                FunctionPermissionDefinition.P_SalesContract_Edit,
                FunctionPermissionDefinition.P_SalesContract_Audit,
                FunctionPermissionDefinition.P_SalesReturn_Edit,
                FunctionPermissionDefinition.P_SalesReturn_Audit,
            ]);
        } else {
            return Engine.profile.permissionMgr.hasPermission([
                FunctionPermissionDefinition.P_SalesContract_Edit,
                FunctionPermissionDefinition.P_SalesReturn_Edit,
            ]);
        }
    }

    static hasSalesRelatedOrderAuditPermission() {
        if (Engine.getOrgSettings().enableSalesRelatedOrdersQuickMode) {
            // 快捷模式下，编辑和审核同等权利
            return Engine.profile.permissionMgr.hasPermission([
                FunctionPermissionDefinition.P_SalesContract_Edit,
                FunctionPermissionDefinition.P_SalesContract_Audit,
                FunctionPermissionDefinition.P_SalesReturn_Edit,
                FunctionPermissionDefinition.P_SalesReturn_Audit,
            ]);
        } else {
            return Engine.profile.permissionMgr.hasPermission([
                FunctionPermissionDefinition.P_SalesContract_Audit,
                FunctionPermissionDefinition.P_SalesReturn_Edit,
            ]);
        }
    }

    // 采购单相关权限
    static hasPurchaseRelatedOrderReadPermission() {
        return Engine.profile.permissionMgr.hasPermission([
            FunctionPermissionDefinition.P_PurchaseContract_Read,
            FunctionPermissionDefinition.P_PurchaseReturn_Read,
        ]);
    }

    // 销售单相关权限
    static hasPurchaseRelatedOrderEditPermission() {
        if (Engine.getOrgSettings().enablePurchaseRelatedOrdersQuickMode) {
            // 快捷模式下，编辑和审核同等权利
            return Engine.profile.permissionMgr.hasPermission([
                FunctionPermissionDefinition.P_PurchaseContract_Edit,
                FunctionPermissionDefinition.P_PurchaseContract_Audit,
                FunctionPermissionDefinition.P_PurchaseReturn_Edit,
                FunctionPermissionDefinition.P_PurchaseReturn_Audit,
            ]);
        } else {
            return Engine.profile.permissionMgr.hasPermission([
                FunctionPermissionDefinition.P_PurchaseContract_Edit,
                FunctionPermissionDefinition.P_PurchaseReturn_Edit,
            ]);
        }
    }

    static hasPurchaseRelatedOrderAuditPermission() {
        if (Engine.getOrgSettings().enablePurchaseRelatedOrdersQuickMode) {
            // 快捷模式下，编辑和审核同等权利
            return Engine.profile.permissionMgr.hasPermission([
                FunctionPermissionDefinition.P_PurchaseContract_Edit,
                FunctionPermissionDefinition.P_PurchaseContract_Audit,
                FunctionPermissionDefinition.P_PurchaseReturn_Edit,
                FunctionPermissionDefinition.P_PurchaseReturn_Audit,
            ]);
        } else {
            return Engine.profile.permissionMgr.hasPermission([
                FunctionPermissionDefinition.P_PurchaseContract_Audit,
                FunctionPermissionDefinition.P_PurchaseReturn_Audit,
            ]);
        }
    }

    // 采购单相关权限
    static hasDealingFundRelatedOrderReadPermission() {
        return Engine.profile.permissionMgr.hasPermission([
            FunctionPermissionDefinition.P_DealingReceive_Read,
            FunctionPermissionDefinition.P_DealingPayment_Read,
        ]);
    }

    // 销售单相关权限
    static hasDealingFundRelatedOrderEditPermission() {
        if (Engine.getOrgSettings().enableQuickFundType) {
            // 快捷模式下，编辑和审核同等权利
            return Engine.profile.permissionMgr.hasPermission([
                FunctionPermissionDefinition.P_DealingReceive_Edit,
                FunctionPermissionDefinition.P_DealingReceive_Audit,
                FunctionPermissionDefinition.P_DealingPayment_Edit,
                FunctionPermissionDefinition.P_DealingPayment_Audit,
            ]);
        } else {
            return Engine.profile.permissionMgr.hasPermission([
                FunctionPermissionDefinition.P_DealingReceive_Edit,
                FunctionPermissionDefinition.P_DealingPayment_Edit,
            ]);
        }
    }

    static hasDealingFundRelatedOrderAuditPermission() {
        if (Engine.getOrgSettings().enableQuickFundType) {
            // 快捷模式下，编辑和审核同等权利
            return Engine.profile.permissionMgr.hasPermission([
                FunctionPermissionDefinition.P_DealingReceive_Edit,
                FunctionPermissionDefinition.P_DealingReceive_Audit,
                FunctionPermissionDefinition.P_DealingPayment_Edit,
                FunctionPermissionDefinition.P_DealingPayment_Audit,
            ]);
        } else {
            return Engine.profile.permissionMgr.hasPermission([
                FunctionPermissionDefinition.P_DealingReceive_Audit,
                FunctionPermissionDefinition.P_DealingPayment_Audit,
            ]);
        }
    }

    static hasDirectFundOrderReadPermission() {
        return Engine.profile.permissionMgr.hasPermission([
            FunctionPermissionDefinition.P_DirectFundIn_Read,
            FunctionPermissionDefinition.P_DirectFundOut_Read,
        ]);
    }

    static hasDirectFundOrderEditPermission() {
        if (Engine.getOrgSettings().enableQuickFundType) {
            // 快捷模式下，编辑和审核同等权利
            return Engine.profile.permissionMgr.hasPermission([
                FunctionPermissionDefinition.P_DirectFundIn_Edit,
                FunctionPermissionDefinition.P_DirectFundIn_Audit,
                FunctionPermissionDefinition.P_DirectFundOut_Edit,
                FunctionPermissionDefinition.P_DirectFundOut_Audit,
            ]);
        } else {
            return Engine.profile.permissionMgr.hasPermission([
                FunctionPermissionDefinition.P_DirectFundIn_Edit,
                FunctionPermissionDefinition.P_DirectFundOut_Edit,
            ]);
        }
    }

    static hasDirectFundOrderAuditPermission() {
        if (Engine.getOrgSettings().enableQuickFundType) {
            // 快捷模式下，编辑和审核同等权利
            return Engine.profile.permissionMgr.hasPermission([
                FunctionPermissionDefinition.P_DirectFundIn_Edit,
                FunctionPermissionDefinition.P_DirectFundIn_Audit,
                FunctionPermissionDefinition.P_DirectFundOut_Edit,
                FunctionPermissionDefinition.P_DirectFundOut_Audit,
            ]);
        } else {
            return Engine.profile.permissionMgr.hasPermission([
                FunctionPermissionDefinition.P_DirectFundIn_Audit,
                FunctionPermissionDefinition.P_DirectFundOut_Audit,
            ]);
        }
    }
}
